<?php

namespace App\Http\Controllers\System;

use App\Extension\TreeGenerator;
use App\Models\Role;
use App\Models\Permission;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PermissionController extends Controller
{
    /**
     * 获取所有权限信息
     *
     * Date: 2018/1/26
     * @author George
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        $permissions = Permission::select(['code', 'name', 'parent_code', 'permissiontable_id', 'permissiontable_type'])->get();
		$permissionsTree = new TreeGenerator($permissions, 'code', 'parent_code', '');
        return $this->success($permissionsTree->tree);
    }

    /**
     * 获取单个权限信息
     *
     * Date: 2018/1/26
     * @author George
     * @param Permission $permission
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(Permission $permission)
    {
        return $this->success($permission);
    }

    /**
     * 创建权限
     *
     * Date: 2018/1/26
     * @author George
     * @param Request $request
     * @deprecated 实际业务中，管理员不能添加权限
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     */
    public function store(Request $request)
    {
        $attributes = $this->validate($request, [
            'code' => 'required',
            'name' => 'required',
            'permissiontable_id' => 'required',
            'permissiontable_type' => 'required'
        ]);

        $permission = $this->serialization($attributes);

        $permission->saveOrFail();

        return $this->stored($permission);
    }

    /**
     * 更新权限信息
     *
     * Date: 2018/1/26
     * @author George
     * @param Request $request
     * @param Permission $permission
     * @deprecated 实际业务中，管理员不能修改权限
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     */
    public function update(Request $request, Permission $permission)
    {
        $attributes = $this->validate($request, [
            'code' => 'required',
            'name' => 'required',
            'permissiontable_id' => 'required',
            'permissiontable_type' => 'required'
        ]);

        foreach ($attributes as $key => $value) {
            $permission->{$key} = $value;
        }

        $permission->saveOrFail();

        return $this->updated($permission);
    }

    /**
     * Date: 2018/1/26
     * @author George
     * @param Permission $permission
     * @deprecated 实际业务中，管理员不能删除
     */
    public function destroy(Permission $permission)
    {

    }

    /**
     * 根据角色编码获取权限信息
     *
     * Date: 2018/1/26
     * @author George
     * @param string $role_code
     * @return mixed
     */
    public function getByRole(string $role_code)
    {
        $role = Role::findByCode($role_code);

        return optional($role)->permissions;
    }

    /**
     * 序列化属性为模型
     *
     * Date: 2018/1/26
     * @author George
     * @param array $attributes
     * @return Permission
     */
    private function serialization(array $attributes): Permission
    {
        $permission = new Permission();

        foreach ($attributes as $key => $value) {
            $permission->{$key} = $value;
        }

        return $permission;
    }
}
